<?php

namespace App\Http\Controllers\Home;

use App\Models\Bonus;
use App\Models\Role;
use App\Models\ScanLog;
use App\Repositories\BonusRepository;
use App\Repositories\UsersRepository;
use App\Repositories\WechatRepository;
use App\User;
use App\ZL\Controllers\ApiBaseController;
use App\ZL\Library\Curl;
use App\ZL\Library\ErrorCode;
use App\ZL\ORG\Xcx\Common;
use EasyWeChat\Foundation\Application;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserCenterController extends BaseController
{
    protected $user;
    protected $wechatRepository;

    public function __construct(UsersRepository $usersRepository)
    {
        parent::__construct();

        $this->user = $usersRepository;
    }

    public function goAuthorize(Request $request)
    {
        if($this->shop->wechat->is_auth==0){
            ScanLog::create([
                'shop_id'=>$this->shop->id,
                'user_id'=>0,
                'qrcode_id'=>$this->qrcode->id,
                'env'=>json_encode($GLOBALS['_SERVER']),
                'created_at'=>date('Y-m-d H;i:s')
            ]);
            $this->qrcode->scan_number ++;
            $this->qrcode->save();


            if($this->qrcode->type==1){
                $_url = $this->qrcode->url;
                if(strstr($_url,'http')==false){
                    $_url = 'http://'.$_url;
                }
                return redirect($_url);
            }else{
                echo ('未定义方法');
            }
        }
        $config_arr = $this->easyOptions;
        //引导用户进入授权页面同意授权，获取code
        $redirect_url = route('wechat.callback',['qrcode_id'=>$request->qrcode_id]);
//        $redirect_url = urlencode($redirect_url);
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$config_arr['app_id'].'&redirect_uri='.$redirect_url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
//        header("location:$url");
//        dd($url);
        return redirect($url);
    }

    public function callback($qrcode_id=null, Request $request)
    {
        $config_arr = $this->easyOptions;
//        $code = $_GET['code'];
        $code = $request->code;
        $state = $request->state;
        if (!$code || !$state){
            dd('授权失败');
        }
        //通过code换取网页授权access_token（与基础支持中的access_token不同）
        $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $config_arr['app_id'] . '&secret=' .  $config_arr['secret'] . '&code=' . $code . '&grant_type=authorization_code';
        $wechat_info = Curl::get($token_url);
        $access_token = json_decode($wechat_info,true);
        if(!isset($access_token['access_token'])){
            dump('授权失败');
            dd($wechat_info);
        }else{

//        dump(session('access_token'));die;
            //获取openid完毕
            $user = $this->user->checkHasAndSaveGetInfoByWechat($access_token['access_token'],$access_token['openid'],$this->shop->id);

            $user->qrcode;
            if($this->qrcode){
                ScanLog::create([
                    'shop_id'=>$this->shop->id,
                    'user_id'=>$user->id,
                    'qrcode_id'=>$this->qrcode->id,
                    'env'=>json_encode($GLOBALS['_SERVER']),
                    'created_at'=>date('Y-m-d H;i:s')
                ]);
                $this->qrcode->scan_number ++;
                $this->qrcode->save();


                if($this->qrcode->type==1){
                    $_url = $this->qrcode->url;
                    if(strstr($_url,'http')==false){
                        $_url = 'http://'.$_url;
                    }
                    return redirect($_url);
                }else{
                    echo ('未定义方法');
                }
            }else{
                echo ('未定义方法');
            }
        }

    }
}
